//
//  TopTipsView.m
//  PayManager
//
//  Created by app on 2017/6/14.
//  Copyright © 2017年 HZJiTeng. All rights reserved.
//

#import "TopTipsView.h"

@implementation TopTipsView

- (instancetype)initWithTips:(NSString *)tips withBOOL:(BOOL)index{
    self = [super init];
    if (self) {
        [self createUIWithStr:tips withIndex:index];
    }
    return self;
}

- (void)createUIWithStr:(NSString *)str withIndex:(NSInteger)index{
    self.backgroundColor = index==1?RGBA(52, 160, 255,0.95):RGBA(226, 0, 97, 0.95);
    
    UIImageView *headImageView = [[UIImageView alloc]init];
    headImageView.image = index==1?IMAGE_NAME(@"done"):IMAGE_NAME(@"fail");
    headImageView.bounds = CGRectMake(0, 0, 18, 18);
    headImageView.center = CGPointMake(30, STATUS_BAR_HEIGHT+20+20);
    [self addSubview:headImageView];
    
    UILabel *label = [[UILabel alloc]init];
    label.textColor = [UIColor whiteColor];
    label.text = str;
    label.font = FONT(16);
    label.frame = CGRectMake(50, headImageView.center.y-22, SCREEN_WIDTH-70, 44);
    [self addSubview:label];
    
    //拖动手势
//    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
//                                                    initWithTarget:self
//                                                    action:@selector(handlePan:)];
//    [self addGestureRecognizer:panGestureRecognizer];
}
- (void)show {
    [UIView animateWithDuration:0.5 delay:0.05 usingSpringWithDamping:0.4 initialSpringVelocity:8 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.frame = CGRectMake(0, -20, SCREEN_WIDTH, STATUS_BAR_HEIGHT+44+20);
    } completion:^(BOOL finished) {
        [self performSelector:@selector(remove) withObject:nil afterDelay:1.2];
    }];
}
- (void)remove {
    [UIView animateWithDuration:0.25 animations:^{
        self.frame = CGRectMake(0, -(STATUS_BAR_HEIGHT+44+20), SCREEN_WIDTH, STATUS_BAR_HEIGHT+44+20);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
    CGPoint translation = [recognizer translationInView:self];
    if (translation.y > 0) {
        translation.y = 0;
    }
    
    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan:
            
            break;
            
        case UIGestureRecognizerStateChanged:
            
            break;
            
        case UIGestureRecognizerStateEnded:
            if (translation.y < -15) {
                [self removeSelf];
            }
            [recognizer setTranslation:CGPointZero inView:self];
            break;
            
        default:
            break;
    }
}

- (void)removeSelf {
    [UIView animateWithDuration:0.168 animations:^{
        self.frame = CGRectMake(0, STATUS_BAR_HEIGHT-44, SCREEN_WIDTH, STATUS_BAR_HEIGHT+44);
    } completion:^(BOOL finished) {
        [UIApplication sharedApplication].statusBarHidden = NO;
        [self removeFromSuperview];
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
